﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;

namespace VisionRoc_Measure
{

    public partial class FormLineTool : Form
    {
        HObject m_Image;
        SetLines m_Lines;
        public FormLineTool(SetLines _Lines, HObject _Image)
        {
            try
            {
                SetStyle(ControlStyles.UserPaint, true);
                SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景 
                SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲  
                SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                this.DoubleBuffered = true;//避免时间显示控件闪烁
                InitializeComponent();
                HOperatorSet.CopyImage(_Image, out m_Image);
                //HOperatorSet.GenEmptyObj(out m_Image);
                //m_Image = _Image;
                m_Lines = _Lines;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void FormLineTool_Load(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.SetDraw(this.hWindowControl1.HalconWindow, "margin");
                HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
                HOperatorSet.SetColored(this.hWindowControl1.HalconWindow, 12);
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text += "图像为空，请加载图像！";
                }
                else
                {
                    HTuple Width = new HTuple(), Height = new HTuple();
                    HOperatorSet.GetImageSize(m_Image, out Width, out Height);
                    HOperatorSet.SetSystem("width", Width);
                    HOperatorSet.SetSystem("height", Height);
                    MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
                    m_Image.DispObj(hWindowControl1.HalconWindow);
                    tool_ResultInfo.Text = "图像加载成功。";
                }
                if (m_Lines != null)
                {
                    m_Lines.ReadfLinesParameter();
                    this.propertyGrid1.SelectedObject = m_Lines;
                    this.propertyGrid1.Refresh();
                }
                else
                {
                    tool_ResultInfo.Text += "线段工具模块对象不存在。";
                }
                string result = "";
                Global.m_MultiLanguage.LControls = new List<Control>();
                Global.m_MultiLanguage.GetControls2(this);
                if (!Global.m_MultiLanguage.LoadLanguage(this, out result))
                {
                    MessageBox.Show(result + "\r\n程序当前语言可能异常，如果影响正常使用请重新启动程序", "加载语言", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        /// <summary>
        /// 绘制线段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DrawLines_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空，请加载图像！";
                    MessageBox.Show("图像为空，请加载图像！");
                    return;
                }
                HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                hWindowControl1.Focus();
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "左键绘制，右键结束", "window", 20, 20, 15, "green");
                groupBox1.Enabled = false;
                button2.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                m_Lines.DrawLineRegion.Dispose(); m_Lines.DrawRow1.Dispose(); m_Lines.DrawCol1.Dispose(); m_Lines.DrawRow2.Dispose(); m_Lines.DrawCol2.Dispose();
                MyHOperatorSet.draw_rake(out m_Lines.DrawLineRegion, hWindowControl1.HalconWindow, m_Lines.Elements, m_Lines.DetectHeight, m_Lines.DetectWidth, out m_Lines.DrawRow1, out m_Lines.DrawCol1, out m_Lines.DrawRow2, out m_Lines.DrawCol2);
                if (m_Lines.DrawRow1.D > 0)
                {
                    HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                    HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                    MyHOperatorSet.DispContour(hWindowControl1.HalconWindow, Model.margin, "blue", m_Lines.DrawLineRegion);
                    MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制成功", "window", 20, 20, 15, "green");
                    tool_ResultInfo.Text = $"线段绘制成功。Row1：{Math.Round(m_Lines.DrawRow1.D, 2)}。Col1：{Math.Round(m_Lines.DrawCol1.D, 2)}。Row2：{Math.Round(m_Lines.DrawRow2.D, 2)}。Col2：{Math.Round(m_Lines.DrawCol2.D, 2)}。";
                    groupBox1.Enabled = true;
                    button2.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                }
                else
                {
                    tool_ResultInfo.Text = "线段绘制失败！";
                    MessageBox.Show("线段绘制失败！");
                    groupBox1.Enabled = true;
                    button2.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                groupBox1.Enabled = true;
            }
        }

        /// <summary>
        /// 清除线段区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Clear_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                HOperatorSet.GenEmptyObj(out m_Lines.DrawLineRegion);
                HOperatorSet.CountObj(m_Lines.DrawLineRegion, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "绘制线段区域已清空";
                }
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            }
            catch
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HOperatorSet.CountObj(m_Image, out HTuple tuple1);
            if (tuple1.I == 0)
            {
                tool_ResultInfo.Text = "图像为空，请加载图像！";
                MessageBox.Show("图像为空，请加载图像！");
                return;
            }
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            if (m_Lines.DrawRow1.D > 0 && m_Lines.DrawRow2.D > 0)
            {
                m_Lines.Fit_Lines(m_Image, hWindowControl1.HalconWindow, m_Lines.DrawRow1, m_Lines.DrawCol1, m_Lines.DrawRow2, m_Lines.DrawCol2);
                if (m_Lines.FitRow1.D > 0 && m_Lines.FitRow2.D > 0)
                {
                    tool_ResultInfo.Text = $"拟合线段成功。【FitRow1：{Math.Round(m_Lines.FitRow1.D, 2)}】。【FitCol1：{Math.Round(m_Lines.FitCol1.D, 2)}】。【FitRow2:{Math.Round(m_Lines.FitRow2.D, 2)}】。【FitCol2：{Math.Round(m_Lines.FitCol2.D, 2)}】。";
                }
            }
        }

        /// <summary>
        /// 保存参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SaveParameter_Click(object sender, EventArgs e)
        {
            try
            {
                m_Lines.WriteLinesParameter();
                MessageBox.Show("参数保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            if (m_Lines != null)
            {
                m_Lines.ReadfLinesParameter();
                this.propertyGrid1.SelectedObject = m_Lines;
                this.propertyGrid1.Refresh();
                tool_ResultInfo.Text = "刷新成功";
            }
        }

        private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            MyHOperatorSet.ZoomImage(hWindowControl1, m_Image, e);
        }

        private void 适应图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HTuple Width = new HTuple(), Height = new HTuple();
            HOperatorSet.GetImageSize(m_Image, out Width, out Height);
            MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
            m_Image.DispObj(hWindowControl1.HalconWindow);
        }

        private void 保存原图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.SaveImage(m_Image);
        }

        private void 保存窗口图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.SaveWindowDump(hWindowControl1.HalconWindow);
        }
    }
}
